{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/140_ee_to_xarray.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "**Converting Earth Engine images to an Xarray Dataset**\n",
    "\n",
    "This notebook demonstrates how to convert Earth Engine images to an Xarray Dataset using [xee](https://github.com/google/Xee)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install -U geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_initialize()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4",
   "metadata": {},
   "source": [
    "Opening the [ERA5-Land hourly dataset](https://developers.google.com/earth-engine/datasets/catalog/ECMWF_ERA5_LAND_HOURLY) in Earth Engine and converting it to an Xarray Dataset. This is a huge dataset and it may take a minute or two to load. Please be patient."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "ds = geemap.ee_to_xarray(\"ECMWF/ERA5_LAND/HOURLY\", n_images=100)\n",
    "ds"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "Open all bands in a specific projection and spatial resolution. Similarly, it may take a minute or two to load."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "ds = geemap.ee_to_xarray(\n",
    "    \"ECMWF/ERA5_LAND/HOURLY\", crs=\"EPSG:4326\", scale=0.25, n_images=100\n",
    ")\n",
    "ds"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8",
   "metadata": {},
   "source": [
    "Open an ImageCollection (maybe, with EE-side filtering or processing):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = ee.ImageCollection(\"ECMWF/ERA5_LAND/HOURLY\").filterDate(\n",
    "    \"1992-10-05\", \"1993-03-31\"\n",
    ")\n",
    "ds = geemap.ee_to_xarray(dataset, crs=\"EPSG:4326\", scale=0.25)\n",
    "ds"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10",
   "metadata": {},
   "source": [
    "Open an ImageCollection with a specific EE projection or geometry:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = ee.ImageCollection(\"ECMWF/ERA5_LAND/HOURLY\").filterDate(\n",
    "    \"1992-10-05\", \"1993-03-31\"\n",
    ")\n",
    "geometry = ee.Geometry.Rectangle(113.33, -43.63, 153.56, -10.66)\n",
    "ds = geemap.ee_to_xarray(\n",
    "    dataset, projection=dataset.first().select(0).projection(), geometry=geometry\n",
    ")\n",
    "ds"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12",
   "metadata": {},
   "source": [
    "Opening a single image:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "image = ee.Image(\"LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318\")\n",
    "ds = geemap.ee_to_xarray(image)\n",
    "ds"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14",
   "metadata": {},
   "source": [
    "Open multiple ImageCollections into one xarray.Dataset, all with the same projection. This one may take a few minutes to load."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "ds = geemap.ee_to_xarray(\n",
    "    dataset=[\"ECMWF/ERA5_LAND/HOURLY\", \"NASA/GDDP-CMIP6\"],\n",
    "    n_images=100,\n",
    "    crs=\"EPSG:4326\",\n",
    "    scale=0.25,\n",
    ")\n",
    "ds"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
